package iscript.gameFramework.GameLite.content 
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.data.DataLoaderVars;
	import com.greensock.loading.data.ImageLoaderVars;
	import com.greensock.loading.data.LoaderMaxVars;
	import com.greensock.loading.data.SWFLoaderVars;
	import com.greensock.loading.data.XMLLoaderVars;
	import com.greensock.loading.DataLoader;
	import com.greensock.loading.ImageLoader;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.LoaderStatus;
	import com.greensock.loading.SWFLoader;
	import com.greensock.loading.XMLLoader;
	import flash.events.Event;
	import flash.net.URLRequest;
	import iscript.gameFramework.GameLite.Game;

	
	//TODO: DataLoader support  urlOrRequest,  todo  SWFLoader XMLloader ImageLoader   
	
	public class GameContentLoader 
	{
		
		private var _contentLoader:LoaderMax;
		private var _merget:Boolean;
		public function get LoaderEntity():LoaderMax { return _contentLoader; }
		
		private var _contentKey:Vector.<String> = new Vector.<String>();
		
		/**
		 * 独立健全加载器,会检测Content是否已有加载资源存在,避免重复加载，[merget=true]合并资源到Content。
		 * @param	merget  
		 * @param	gamecontentLoaderVars
		 */
		public function GameContentLoader(merget:Boolean=true,gamecontentLoaderVars:LoaderMaxVars=null) 
		{
			     _merget = merget;
		         _contentLoader = new LoaderMax(gamecontentLoaderVars);		
				 _contentLoader.addEventListener(LoaderEvent.CHILD_COMPLETE, _ChileAddToGameContentLogic);
		}
		
		private function _ChileAddToGameContentLogic(e:LoaderEvent):void 
		{
			 if(_merget){
			   if (e.target is SWFLoader) {
				   Game.Content.addSWFLoaderObject(SWFLoader(e.target).vars[GameContent.GamneContentKey],SWFLoader(e.target));
				   }
			   if (e.target is ImageLoader) {
			       Game.Content.addImageLoaderObject(ImageLoader(e.target).vars[GameContent.GamneContentKey],ImageLoader(e.target));
			       }
			   if (e.target is XMLLoader) {
			       Game.Content.addXMLLoaderObject(XMLLoader(e.target).vars[GameContent.GamneContentKey],XMLLoader(e.target));
			       }
			   if (e.target is DataLoader && String(e.target).indexOf("DataLoader")!=-1) {
			       Game.Content.addDataLoaderObject(DataLoader(e.target).vars[GameContent.GamneContentKey],DataLoader(e.target));
			       }
			 }
		}
		
		
		public function addSwf(urlOrRequest:*,param:SWFLoaderVars = null):void
		{
			var key:String = _getKey(urlOrRequest);
			if (!Game.Content.containGameContent(key)){
		     if (param == null)  param = new SWFLoaderVars();
					param.vars[GameContent.GamneContentKey] = key;
					var  swfload:SWFLoader = new SWFLoader(key, param);
			        _contentLoader.append(swfload);
			}else {
				 _contentLoader.append(Game.Content.getSWF(key));
			}
		}
		
		
		public function addImage(urlOrRequest:*, param:ImageLoaderVars = null):void { 
			 var key:String = _getKey(urlOrRequest);
			 if (!Game.Content.containGameContent(key)){
		         if (param == null)  param = new ImageLoaderVars();
					param.vars[GameContent.GamneContentKey] = key;
					var  imageload:ImageLoader = new ImageLoader(key, param);
			        _contentLoader.append(imageload);
			   }else {
				    _contentLoader.append(Game.Content.getImage(key));
			   }
			}
		
		public function addXml(urlOrRequest:*, param:XMLLoaderVars = null):void { 
			    var key:String = _getKey(urlOrRequest);			    
			   if (!Game.Content.containGameContent(key)){
		         if (param == null)  param = new XMLLoaderVars();
					param.vars[GameContent.GamneContentKey] = key;
					var  xmload:XMLLoader = new XMLLoader(key, param);
			        _contentLoader.append(xmload);
			   }else {
					 _contentLoader.append(Game.Content.getXMLoader(key))
				 }			
			}
		 
			
       public function addDataLoader(urlOrRequest:*, param:DataLoaderVars = null):void {
		         var key:String = _getKey(urlOrRequest);
					 
		         if (!Game.Content.containGameContent(key)){
					 if (param == null)  param = new  DataLoaderVars();
						param.vars[GameContent.GamneContentKey] = key;
						var  dataload:DataLoader = new DataLoader(urlOrRequest, param);
						_contentLoader.append(dataload);
			     }else {
					 _contentLoader.append(Game.Content.getDataLoader(key))
				 }
		   }
			
		private  function _getKey(urlOrRequest:*) :String {
			     var key:String;
				 if (urlOrRequest is URLRequest) { 
					 key = URLRequest(urlOrRequest).url } else {
					 key = urlOrRequest;
					 }
					 return key;
			   }
		   
		public function load():void {
			   _contentLoader.load();
			}
		 
	}

}